package com.example.xm;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.xm.ui.discover.DiscoverFragment;
import com.example.xm.ui.home.HomeFragment;
import com.example.xm.ui.own.OwnFragment;
import com.example.xm.ui.own.activity.LoginActivity;
import com.example.xm.ui.shop.ShopFragment;
import com.example.xm.utiles.FragmentUtils;
import com.example.xm.utiles.SpUtils;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.tab)
    TabLayout tab;
    private FragmentManager manager;
    private HomeFragment homeFragment;
    private DiscoverFragment discoverFragment;
    private ShopFragment shopFragment;
    private OwnFragment ownFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initPermiss();
        initView();
    }

    //请求权限
    private void initPermiss() {
        PermissionsUtil.requestPermission(this, new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) {
                Toast.makeText(MainActivity.this, "请求权限成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void permissionDenied(@NonNull String[] permission) {
                Toast.makeText(MainActivity.this, "请求权限失败", Toast.LENGTH_SHORT).show();
            }
        }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_NETWORK_STATE});
    }

    private void initView() {
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        homeFragment = new HomeFragment();
        discoverFragment = new DiscoverFragment();
        shopFragment = new ShopFragment();
        ownFragment = new OwnFragment();
        transaction.add(R.id.fl, homeFragment);
        transaction.add(R.id.fl, discoverFragment);
        transaction.add(R.id.fl, shopFragment);
        transaction.add(R.id.fl, ownFragment);
        transaction.show(homeFragment);
        transaction.hide(discoverFragment);
        transaction.hide(shopFragment);
        transaction.hide(ownFragment);
        transaction.commit();
        tab.addTab(tab.newTab().setText("首页").setIcon(R.drawable.tab1));
        tab.addTab(tab.newTab().setText("发现").setIcon(R.drawable.tab2));
        tab.addTab(tab.newTab().setText("商城").setIcon(R.drawable.tab3));
        tab.addTab(tab.newTab().setText("我们").setIcon(R.drawable.tab4));
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                TextView textView = new TextView(MainActivity.this);
//                float v = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 16, getResources().getDisplayMetrics());
//                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,v);
//                textView.setTextColor(getResources().getColor(R.color.colorPrimary));
//                textView.setText(tab.getText());
//                tab.setCustomView(textView);
                switch (tab.getPosition()) {
                    case 0:
                        manager
                                .beginTransaction()
                                .show(homeFragment)
                                .hide(discoverFragment)
                                .hide(shopFragment)
                                .hide(ownFragment)
                                .commit();
                        break;
                    case 1:
                        manager
                                .beginTransaction()
                                .show(discoverFragment)
                                .hide(homeFragment)
                                .hide(shopFragment)
                                .hide(ownFragment)
                                .commit();
                        break;
                    case 2:
                        manager
                                .beginTransaction()
                                .show(shopFragment)
                                .hide(discoverFragment)
                                .hide(homeFragment)
                                .hide(ownFragment)
                                .commit();
                        break;

                    case 3:
                        String token = SpUtils.getInstance().getString("token");
                        if (!TextUtils.isEmpty(token)) {
                            manager
                                    .beginTransaction()
                                    .show(ownFragment)
                                    .hide(discoverFragment)
                                    .hide(shopFragment)
                                    .hide(homeFragment)
                                    .commit();
                        } else {
                            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                            startActivityForResult(intent, 1);
                        }
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //判断当前是否已经登录,如果登录跳转主界面，如果没登录，跳转登录页面
        String token = SpUtils.getInstance().getString("token");
        if (TextUtils.isEmpty(token)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //从登陆界面回来的跳转到主界面的第二个tab
        if (requestCode == 1) {
            manager
                    .beginTransaction()
                    .show(homeFragment)
                    .hide(discoverFragment)
                    .hide(shopFragment)
                    .hide(ownFragment)
                    .commit();
            HomeFragment fragment = (HomeFragment) FragmentUtils.getFragment(HomeFragment.class, MainActivity.this);
            if (fragment != null) {
                fragment.gotoTabByIndex(1);
            }
        }
    }
}